<?php
include_once 'ligneHistorique.class.php';

class Compte {
    
    private $compte_id = -1;
    private $nom = '';
    private $solde = 0;
    
    public function __construct($p_compte_id = -1) {
        if($p_compte_id != -1){
            $res = db_query("SELECT * FROM compte WHERE compte_id = ".$p_compte_id);
            if($res && db_numrows($res)>0){
                $data = db_fetch_array($res);
                
                $this->compte_id = isset($data['compte_id'])?$data['compte_id']:-1;
                $this->nom = isset($data['nom'])?$data['nom']:'';
                $this->solde = isset($data['solde'])?$data['solde']:0;
            }
        }
    }
    
    public function credit($p_date = '', $p_comment = '',$p_credit = 0){
        
        $datetime = (empty($p_date))?time():strtotime($p_date);
        if(is_numeric($p_credit)){
            $this->solde += $p_credit;
            
            $this->addHistoryEntry($p_date,$p_comment,$p_credit,  Mouvement::Credit);
            $this->update();
        }
        return $this->solde;
    }
    
    public function debit($p_date = '', $p_comment = '',$p_debit){
        
        $datetime = (empty($p_date))?time():strtotime($p_date);
        if(!empty($p_comment) && is_numeric($p_debit)){
            $this->solde -= $p_debit;
            
            $this->addHistoryEntry($p_date,$p_comment,$p_debit,  Mouvement::Debit);
            $this->update();
        }
        return $this->solde;
    }
    
    private function addHistoryEntry($p_date,$p_comment,$p_debit, Mouvement $mvt_type){
        
    }
    
    private function update(){
        $res = db_query("UPDATE compte SET nom = '{$this->nom}', solde = {$this->solde} WHERE compte_id = {$this->compte_id}");
        if($res && db_affected_rows()>0){
            return true;
        }
        return false;
    }
    
}
